<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采购订单id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入采购订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经销商id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入经销商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购的车的数量" prop="wholePurchaseNumber">
        <el-input
          v-model="queryParams.wholePurchaseNumber"
          placeholder="请输入采购的车的数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购状态" prop="purchaseStatus">
        <el-select v-model="queryParams.purchaseStatus" placeholder="采购状态" clearable>
          <el-option
            v-for="dict in dict.type.whole_purchase_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="采购的总价" prop="purchasePrice">
        <el-input
          v-model="queryParams.purchasePrice"
          placeholder="请输入采购的总价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMethod">
        <el-select v-model="queryParams.paymentMethod" placeholder="请选择支付方式" clearable>
          <el-option
            v-for="dict in dict.type.pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人id" prop="auditorId">
        <el-input
          v-model="queryParams.auditorId"
          placeholder="请输入审核人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人名称" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditorTime">
        <el-date-picker clearable
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['whole:purchase:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['whole:purchase:edit']"-->
<!--        >审核</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['whole:purchase:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['whole:purchase:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="purchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="false"/>
      <el-table-column label="id" align="center" prop="id" />
<!--      <el-table-column label="采购订单id" align="center" prop="purchaseId" />-->
      <el-table-column label="经销商id" align="center" prop="deptId" />
      <el-table-column label="采购的车的数量" align="center" prop="wholePurchaseNumber" />
      <el-table-column label="采购状态" align="center" prop="purchaseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.whole_purchase_status" :value="scope.row.purchaseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="采购的总价" align="center" prop="purchasePrice" />
      <el-table-column label="支付方式" align="center" prop="paymentMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_type" :value="scope.row.paymentMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="采购备注" align="center" prop="purchaseRemark" />
      <el-table-column label="审核人id" align="center" prop="auditorId" />
      <el-table-column label="审核人名称" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['whole:purchase:edit']"
            :disabled = "!([0, 1, 2].includes(scope.row.purchaseStatus))"
          >审核</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['whole:purchase:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆采购对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购状态" prop="purchaseStatus">
          <el-select v-model="form.purchaseStatus" placeholder="请选择采购状态" :disabled="!(title == '添加车辆采购' || ([0, 1, 2,3].includes(form.purchaseStatus)))">
            <el-option
              v-for="dict in dict.type.whole_purchase_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="paymentMethod">
          <el-select v-model="form.paymentMethod" placeholder="请选择支付方式" >
            <el-option
              v-for="dict in dict.type.pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
              :disabled="!(title == '添加车辆采购' || ([0, 1, 2,3].includes(form.purchaseStatus)))"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="purchaseRemark">
          <el-input v-model="form.purchaseRemark" type="textarea" placeholder="请输入内容" :disabled="!(title == '添加车辆采购' || ([0, 1, 2,3].includes(form.purchaseStatus)))"/>
        </el-form-item>
        <el-divider content-position="center">车辆采购详细信息</el-divider>
<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDmsDealerWholePurchaseDetail">添加</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDmsDealerWholePurchaseDetail">删除</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-table :data="dmsDealerWholePurchaseDetailList" :row-class-name="rowDmsDealerWholePurchaseDetailIndex" @selection-change="handleDmsDealerWholePurchaseDetailSelectionChange" ref="dmsDealerWholePurchaseDetail">-->
<!--          <el-table-column type="selection" width="50" align="center" />-->
<!--          <el-table-column label="序号" align="center" prop="index" width="50"/>-->
<!--          <el-table-column label="车辆VIN" prop="vin" width="350">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.vin" placeholder="请输入车辆VIN" />-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table v-loading="loading" :data="dmsDealerWholePurchaseDetailList" :row-class-name="rowDmsDealerWholePurchaseDetailIndex" @selection-change="handleDmsDealerWholePurchaseDetailSelectionChange" ref="dmsDealerWholePurchaseDetail">
            <el-table-column v-if="this.title =='添加车辆采购' " type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50"/>-->
            <el-table-column label="id" align="center" prop="id" />
            <el-table-column label="车架号VIN" align="center" prop="vin" />
            <el-table-column label="车的名称" align="center" prop="name" />
            <el-table-column label="品牌" align="center" prop="brand" />
            <el-table-column label="店端/厂端的库存" align="center" prop="inventoryType" />
          </el-table>
<!--          <el-table-column label="创建人名称" prop="createUserName" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.createUserName" placeholder="请输入创建人名称" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="修改人名称" prop="updateUserName" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.updateUserName" placeholder="请输入修改人名称" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="版本号" prop="version" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.version" placeholder="请输入版本号" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--        </el-table>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="title == '添加车辆采购' || [0, 1, 2,3].includes(form.purchaseStatus)">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPurchase, getPurchase, delPurchase, addPurchase, updatePurchase } from "@/api/whole/purchase";
import {listInventory} from "@/api/whole/inventory";

export default {
  name: "Purchase",
  dicts: ['whole_purchase_status', 'pay_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDmsDealerWholePurchaseDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆采购表格数据
      purchaseList: [],
      // 车辆采购详细表格数据
      dmsDealerWholePurchaseDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        deptId: null,
        wholePurchaseNumber: null,
        purchaseStatus: null,
        purchasePrice: null,
        paymentMethod: null,
        purchaseRemark: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        purchaseStatus: [
          { required: true, message: "采购状态不能为空", trigger: "change" }
        ],
        paymentMethod: [
          { required: true, message: "支付方式不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆采购列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then(response => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        // purchaseId: null,
        deptId: null,
        companyId: null,
        wholePurchaseNumber: null,
        purchaseStatus: null,
        purchasePrice: null,
        paymentMethod: null,
        purchaseRemark: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      this.dmsDealerWholePurchaseDetailList = [];
      this.resetForm("form");

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 页面多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // // 新增弹窗多选框选中数据
    // handleSelectionChange02(selection) {
    //   this.checkedDmsDealerWholePurchaseDetail = selection.map(item => item.index)
    //   this.vehicleids = selection.map(item => item.id)
    //   this.vehiclevins = selection.map(item => item.vin)
    //   this.vehiclesingle = selection.length!==1
    //   this.vehiclemultiple = !selection.length
    // },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆采购";
      // 查询车辆库存表
      this.getVehicleList();
    },
    getVehicleList() {
      this.loading = true;
      this.queryParams.inventoryType = "厂端";
      listInventory(this.queryParams).then(response => {
        this.dmsDealerWholePurchaseDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPurchase(id).then(response => {
        this.form = response.data;
        this.dmsDealerWholePurchaseDetailList = response.data.dmsDealerWholePurchaseDetailList;
        this.open = true;
        this.title = "修改车辆采购";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            //把展示的数据都提交（不支持更改）
            this.form.dmsDealerWholePurchaseDetailList = this.dmsDealerWholePurchaseDetailList;
            updatePurchase(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const checkedDmsDealerWholePurchaseDetail = this.checkedDmsDealerWholePurchaseDetail;
            this.form.dmsDealerWholePurchaseDetailList = this.dmsDealerWholePurchaseDetailList.filter(function(item) {
              return checkedDmsDealerWholePurchaseDetail.indexOf(item.index) != -1
            });
            //把勾选的车辆作为参数
            addPurchase(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆采购编号为"' + ids + '"的数据项？').then(function() {
        return delPurchase(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 车辆采购详细序号 */
    rowDmsDealerWholePurchaseDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 车辆采购详细添加按钮操作 */
    handleAddDmsDealerWholePurchaseDetail() {
      let obj = {};
      obj.vin = "";
      obj.createUserName = "";
      obj.updateUserName = "";
      obj.version = "";
      obj.deleteFlag = "";
      this.dmsDealerWholePurchaseDetailList.push(obj);
    },
    /** 车辆采购详细删除按钮操作 */
    handleDeleteDmsDealerWholePurchaseDetail() {
      if (this.checkedDmsDealerWholePurchaseDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的车辆采购详细数据");
      } else {
        const dmsDealerWholePurchaseDetailList = this.dmsDealerWholePurchaseDetailList;
        const checkedDmsDealerWholePurchaseDetail = this.checkedDmsDealerWholePurchaseDetail;
        this.dmsDealerWholePurchaseDetailList = dmsDealerWholePurchaseDetailList.filter(function(item) {
          return checkedDmsDealerWholePurchaseDetail.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDmsDealerWholePurchaseDetailSelectionChange(selection) {
      this.checkedDmsDealerWholePurchaseDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('whole/purchase/export', {
        ...this.queryParams
      }, `purchase_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
